package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"meituan-waimai/config"
)

var (
	err error
	DB  *gorm.DB
)

func Init() {
	mData := config.ConfigData.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True", mData.Username, mData.Password, mData.Host, mData.Port, mData.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("Mysql数据库连接失败")
	} else {
		log.Println("Mysql数据库连接成功")
	}
	err = DB.AutoMigrate(&User{}, &Merchant{}, &MerchantRealName{}, &Order{}, &Goods{}, &Coupon{}, &Delivery{}, &OrderEvaluate{}, &ShoppingCard{})
	if err != nil {
		panic("Mysql数据库迁移失败")
	}
}
